// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Scheduler.java

package presenter;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.plaf.basic.BasicInternalFrameUI;

// Referenced classes of package presenter:
//            SongFile, SimpleVariableFile, Bible, SongVerse

public class Scheduler extends JInternalFrame
    implements MouseListener, KeyListener, ActionListener
{

    public Scheduler(SongFile allSongs[])
    {
        this(null, allSongs);
    }

    public Scheduler(SongFile songs[], SongFile allSongs[])
    {
        this.allSongs = allSongs;
        this.songs = new ArrayList();
        if(songs != null)
        {
            for(int i = 0; i < songs.length; i++)
                this.songs.add(songs[i]);

        }
        song = (SongFile[])this.songs.toArray(new SongFile[1]);
        songList = new JList(song);
        scrollPane = new JScrollPane();
        scrollPane.getViewport().setView(songList);
        scrollPane.setVisible(true);
        add(scrollPane);
        songList.addMouseListener(this);
        update = false;
        try
        {
            ((BasicInternalFrameUI)getUI()).getNorthPane().setPreferredSize(new Dimension(0, 0));
        }
        catch(Exception e) { }
        addKeyListener(this);
        t = new Timer(100, this);
        t.start();
    }

    public void setSchedule(File f, SongFile songs[], Bible b)
    {
        SimpleVariableFile svf = new SimpleVariableFile(f);
        this.songs = new ArrayList();
        bible = b;
        this.f = f;
        SongFile song = new SongFile("   ");
        for(int i = 0; i < svf.getNumberOfVariables(); i++)
        {
            if(svf.exists((new StringBuilder()).append("SongColor").append(i).toString()))
            {
                String colors = svf.getVariable((new StringBuilder()).append("SongColor").append(i).toString());
                int red = Integer.parseInt(colors.substring(1, colors.indexOf('G')));
                int green = Integer.parseInt(colors.substring(colors.indexOf('G') + 1, colors.indexOf('B')));
                int blue = Integer.parseInt(colors.substring(colors.indexOf('B') + 1, colors.length()));
                Color color = new Color(red, green, blue);
                song.setColor(color);
            }
            if(svf.getVariable((new StringBuilder()).append("Song").append(i).toString()).equals("Scripture"))
            {
                if(svf.exists((new StringBuilder()).append("Scripture").append(i).toString()))
                {
                    SongVerse svs[] = b.getVerses(svf.getVariable((new StringBuilder()).append("Scripture").append(i).toString()));
                    SongFile scripture = new SongFile(svs[0].getType());
                    scripture.setScriptureString(svf.getVariable((new StringBuilder()).append("Scripture").append(i).toString()));
                    scripture.setScripture(true);
                    scripture.setVerses(svs);
                    this.songs.add(scripture);
                }
            } else
            {
                int id = Integer.parseInt(svf.getVariable((new StringBuilder()).append("Song").append(i).toString()));
                if(svf.exists((new StringBuilder()).append("SongVideo").append(i).toString()))
                {
                    String vid = svf.getVariable((new StringBuilder()).append("SongVideo").append(i).toString());
                    title = vid.substring(0, vid.lastIndexOf("."));
                    SongFile video = new SongFile(title);
                    video.setVideo(vid);
                    this.songs.add(video);
                }
                for(int j = 0; j < songs.length; j++)
                {
                    if(songs[j].getNumber() != id)
                        continue;
                    if(svf.exists((new StringBuilder()).append("SongVideo").append(i).toString()))
                    {
                        song = songs[j];
                        song.setVideo(svf.getVariable((new StringBuilder()).append("SongVideo").append(i).toString()));
                        this.songs.add(song);
                    } else
                    {
                        this.songs.add(songs[j]);
                    }
                }

            }
            if(svf.exists((new StringBuilder()).append("SongBackground").append(i).toString()))
                song.setBackground(svf.getVariable((new StringBuilder()).append("SongBackground").append(i).toString()));
        }

        this.song = (SongFile[])this.songs.toArray(this.song);
        songList.setListData(this.song);
    }

    public SongFile[] getSchedule()
    {
        return (SongFile[])songs.toArray(new SongFile[1]);
    }

    public void setSchedule(SongFile song[])
    {
        for(int i = 0; i < song.length; i++)
            songs.add(song[i]);

        songList.setListData(song);
    }

    public void addSong(SongFile sf)
    {
        songs.add(sf);
        song = (SongFile[])songs.toArray(song);
        songList.setListData(song);
    }

    public SongFile getActive()
    {
        if(songList.getSelectedIndex() != -1)
            return (SongFile)songs.get(songList.getSelectedIndex());
        else
            return null;
    }

    public void nextSong()
    {
        if(songList.getSelectedIndex() < songs.size() - 1)
            songList.setSelectedIndex(songList.getSelectedIndex() + 1);
    }

    public boolean isUpdated()
    {
        if(update)
        {
            update = false;
            return true;
        } else
        {
            return false;
        }
    }

    public Scheduler clone()
    {
        Scheduler sc = new Scheduler((SongFile[])songs.toArray(new SongFile[1]), allSongs);
        sc.setSchedule((SongFile[])songs.toArray(new SongFile[1]));
        sc.setSize(getSize());
        sc.setVisible(isVisible());
        return sc;
    }

    public void mouseClicked(MouseEvent mouseevent)
    {
    }

    public void mousePressed(MouseEvent arg0)
    {
        requestFocusInWindow();
        update = true;
    }

    public void mouseReleased(MouseEvent mouseevent)
    {
    }

    public void mouseEntered(MouseEvent mouseevent)
    {
    }

    public void mouseExited(MouseEvent mouseevent)
    {
    }

    public void keyTyped(KeyEvent keyevent)
    {
    }

    public void openSchedule()
    {
        JFileChooser fc = new JFileChooser(new File("./"));
        fc.setSelectedFile(new File("Schedule.txt"));
        int returnVal = fc.showOpenDialog(this);
        if(returnVal == 0)
            setSchedule(fc.getSelectedFile(), allSongs, bible);
    }

    public void saveSchedule()
    {
        JFileChooser fc = new JFileChooser(new File("./"));
        fc.setSelectedFile(new File("Schedule.txt"));
        int returnVal = fc.showSaveDialog(this);
        if(returnVal == 0)
        {
            File file = fc.getSelectedFile();
            SimpleVariableFile svf = new SimpleVariableFile();
            ArrayList output = new ArrayList();
            ArrayList vars = new ArrayList();
            int lim = 0;
            for(int i = 0; i < songs.size(); i++)
            {
                SongFile sf = (SongFile)songs.get(i);
                if(sf.hasVideo())
                {
                    vars.add((new StringBuilder()).append("SongVideo").append(i).toString());
                    output.add(sf.getVideo());
                }
                if(sf.hasCustomBackground())
                {
                    vars.add((new StringBuilder()).append("SongBackground").append(i).toString());
                    output.add(sf.getBackground());
                }
                if(sf.isScripture())
                {
                    vars.add((new StringBuilder()).append("Scripture").append(i).toString());
                    output.add(sf.getScriptureString());
                    vars.add((new StringBuilder()).append("Song").append(i).toString());
                    output.add("Scripture");
                } else
                {
                    vars.add((new StringBuilder()).append("Song").append(i).toString());
                    output.add((new StringBuilder()).append("").append(sf.getNumber()).toString());
                }
            }

            svf.update((String[])vars.toArray(new String[1]), (String[])output.toArray(new String[1]));
            svf.write(file);
        }
    }

    public void keyPressed(KeyEvent ke)
    {
        int x = ke.getModifiers();
        int key = ke.getKeyCode();
        if(x == 2 && key == 83)
            saveSchedule();
        if(x == 0 && key == 127)
        {
            songs.remove(songList.getSelectedIndex());
            song = (SongFile[])songs.toArray(song);
            songList.setListData(song);
        }
        if(x == 2 && key == 79)
            openSchedule();
        if(x == 1 && key == 38)
            shiftUp(songList.getSelectedIndex());
        if(x == 1 && key == 40)
            shiftDown(songList.getSelectedIndex());
    }

    public void shiftDown(int index)
    {
        if(index < songs.size() - 1)
        {
            SongFile temp = (SongFile)songs.get(index);
            songs.set(index, songs.get(index + 1));
            songs.set(index + 1, temp);
            song = (SongFile[])songs.toArray(song);
            songList.setListData(song);
            songList.setSelectedIndex(index + 1);
        }
    }

    public void shiftUp(int index)
    {
        shiftDown(index - 1);
        songList.setSelectedIndex(index - 1);
    }

    public void keyReleased(KeyEvent keyevent)
    {
    }

    public void actionPerformed(ActionEvent actionevent)
    {
    }

    public volatile Object clone()
        throws CloneNotSupportedException
    {
        return clone();
    }

    ArrayList songs;
    SongFile song[];
    JList songList;
    JScrollPane scrollPane;
    Timer t;
    SongFile allSongs[];
    Bible bible;
    File f;
    boolean update;
}
